package com.baiyinet.ui.share;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baiyinet.R;
import com.baiyinet.util.webview.GeneralWebViewClient;

import java.util.ArrayList;

public class ShareMultiPicDialogActivity extends Activity implements View.OnClickListener{


    private TextView tvCancel;
    private LinearLayout layoutWechat;
    private LinearLayout layoutMonments;
    private ArrayList<Uri> imageUris;
    private View layoutCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_pic_share);
        tvCancel = (TextView) findViewById(R.id.share_pic_cancel);
        layoutWechat = (LinearLayout) findViewById(R.id.dialog_pic_share_layout_wechat);
        layoutMonments = (LinearLayout) findViewById(R.id.dialog_pic_share_layout_moments);
        layoutCancel = findViewById(R.id.share_pic_layout_cancel);

        tvCancel.setOnClickListener(this);
        layoutMonments.setOnClickListener(this);
        layoutWechat.setOnClickListener(this);
        layoutCancel.setOnClickListener(this);

        if (getIntent() != null) {
            imageUris = getIntent().getParcelableArrayListExtra("imageUris");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share_pic_layout_cancel:
            case R.id.share_pic_cancel:
                finish();
                break;
            case R.id.dialog_pic_share_layout_wechat:
                sendIntent("com.tencent.mm.ui.tools.ShareImgUI");
                break;
            case R.id.dialog_pic_share_layout_moments:
                sendIntent("com.tencent.mm.ui.tools.ShareToTimeLineUI");
                break;
        }
    }

    private void sendIntent(String intentStr) {
        if (imageUris == null) {
            imageUris = new ArrayList<>();
        }

        try {
            Intent shareIntent = new Intent();
            shareIntent.setPackage("com.tencent.mm");
            shareIntent.setComponent(new ComponentName("com.tencent.mm",
                    intentStr));
            shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
            shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
            String text = GeneralWebViewClient.sShareContent;
            ArrayList<String> extraText = new ArrayList<>();
            extraText.add(text);
            shareIntent.putExtra(Intent.EXTRA_TEXT, extraText);
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, text);
            shareIntent.putExtra(Intent.EXTRA_TITLE, text);
            shareIntent.putExtra("Kdescription", text);
            shareIntent.setType("image/*");
            startActivity(Intent.createChooser(shareIntent, GeneralWebViewClient.sShareTitle));
            Toast.makeText(this, "正在打开微信分享...", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(this, "分享失败！请先安装或打开微信", Toast.LENGTH_LONG).show();
        }
    }
}
